package com.gexiaobao.pigeon.ui.fragment.mine.device

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentMyDeviceBinding
import com.gexiaobao.pigeon.ui.adapter.DeviceListAdapter
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.viewmodel.MyDeviceViewModel
import com.yanzhenjie.recyclerview.OnItemMenuClickListener
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 15:12
 * @Describe    : 我的设备
 */
class FragmentMyDevice : BaseFragment<MyDeviceViewModel, FragmentMyDeviceBinding>() {

    private val mAdapter: DeviceListAdapter by lazy { DeviceListAdapter(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolbar.initClose("我的设备") {
            nav().navigateUp()
        }
    }

    override fun initData() {
        super.initData()

        val swipeMenuCreator = SwipeMenuCreator { _, rightMenu, _ ->
            val width = resources.getDimensionPixelSize(R.dimen.dp_70)
            val height = ViewGroup.LayoutParams.MATCH_PARENT
            val deleteItem = SwipeMenuItem(activity)
                .setBackground(R.drawable.round_b_g_5_train_fly_list_delete_red)
                .setTextColor(Color.WHITE)
                .setText(R.string.delete)
                .setTextSize(15)
                .setWidth(width)
                .setHeight(height)
            rightMenu.addMenuItem(deleteItem)
        }
        mDatabind.layoutList.swipeRecyclerView.setSwipeMenuCreator(swipeMenuCreator)

        val mItemMenuClickListener = OnItemMenuClickListener { menuBridge, position ->
            /** 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱 */
            menuBridge.closeMenu()
            /** 左侧还是右侧菜单 */
//            val direction = menuBridge.direction
//            /**  菜单在Item中的Position */
//            val menuPosition = menuBridge.position
            /** 调用删除接口 */
            deleteDevice(mAdapter.data[position].id)
        }
        mDatabind.layoutList.swipeRecyclerView.setOnItemMenuClickListener(mItemMenuClickListener)

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 5f)))

        mDatabind.layoutList.swipeRefreshLayout.init {
            onRefresh()
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                nav().navigateAction(R.id.action_to_device_detail, Bundle().apply {
                    putInt("id", mAdapter.data[position].id)
                    putString("imsi", mAdapter.data[position].imsi)
                })
            }
        }
    }

    private fun deleteDevice(mId: Int) {
        val dialog = RxDialogDefault(context)
        dialog.setTitle("是否删除设备")
        dialog.setSureListener {
            showLoading(getString(R.string.loading))
            mViewModel.deletePigeonDevice(mId)
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()

    }

    override fun onResume() {
        super.onResume()
        onRefresh()
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getDeviceList()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvAddDevice) {
            when (it) {
                mDatabind.tvAddDevice -> {
                    nav().navigateAction(R.id.action_to_add_device, Bundle().apply {
                        putString("title", "添加设备")
                    })
                }
            }
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /**  设备列表  */
        mViewModel.deviceList.observe(viewLifecycleOwner) {
            if (mDatabind.layoutList.swipeRefreshLayout.isRefreshing) {
                mDatabind.layoutList.swipeRefreshLayout.isRefreshing = false
            }
            dismissLoading()
//            if (!it.isEmpty()) {
            mAdapter.setList(it.list)
            mAdapter.notifyDataSetChanged()
//            }
//            loadListData(it, mAdapter, loadsir, swipeRecyclerView, swipeRefreshLayout)
        }
        /** 删除设备 */
        mViewModel.deletePigeonDevice.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                onRefresh()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }
}